package org.yyjkcache.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class ConnectionQueue {
	/**
	 * 阻塞队列
	 */
	@SuppressWarnings("rawtypes")
	private BlockingQueue<Dispatch> queue=new ArrayBlockingQueue<Dispatch>(10);
	private static ConnectionQueue connectionQueue = new ConnectionQueue();

	private ConnectionQueue() {
	}

	/**
	 * 推入
	 * 
	 * @param
	 */
	@SuppressWarnings("rawtypes")
	public synchronized boolean push(Dispatch dispatch) {
		boolean flag = true;
		try {
			queue.add(dispatch);
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}

	/**
	 * 弹出
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public synchronized Dispatch pop() {
		return queue.poll();
	}

	/**
	 * 初始化
	 */
	public synchronized void init(int size) {
		queue = new ArrayBlockingQueue<>(10);
	}

	/**
	 * 线程调度
	 * 
	 * @return
	 */
	public static ConnectionQueue dispatch() {
		return connectionQueue;
	}
	
}
